﻿

#region Using Statements

using UnityEngine;
using System.Collections.Generic;

#endregion


[RequireComponent(typeof(Rigidbody2D))]
/// <summary>
/// 
/// </summary>
public class Birdza : MonoBehaviour
{

    #region Members

    public float forceRate = 300.0f;

    public float baseSpinRate = 50.0f;

    public float velocitySpinRate = 100.0f;

    public float velocitySpinPower = 0.3f;

    public float initialTestForce = 1000.0f;

    public float damping = 0.15f;

    public float currentForce { get; set; }

    float TimeToDeath = -1;

    #endregion


    #region Init

	/// <summary>
    /// 
    /// </summary>
    protected virtual void Awake()
    {

    }


    /// <summary>
    /// 
    /// </summary>
    protected virtual void Start()
    {
        rigidbody2D.AddForce((Vector2.right + Vector2.up) * initialTestForce);
    }

    #endregion


    #region Update

    /// <summary>
    /// 
    /// </summary>
    protected virtual void Update()
    {
        if (TimeToDeath != -1)
        {
            if (TimeToDeath < 0) Destroy(gameObject);
            else TimeToDeath -= Time.deltaTime;
        }
        else
        {
            GameObject Player = GameObject.FindGameObjectWithTag("Player");
            Vector3 diff = new Vector3((Player.transform.position - transform.position).x, 0, 0);
            Vector3 dir = diff.normalized;

            rigidbody2D.AddForce(dir * currentForce * Time.deltaTime);

            currentForce += forceRate * Time.deltaTime;
            currentForce *= Util.DampingCoefficient(damping);

            rigidbody2D.AddTorque((baseSpinRate + velocitySpinRate * Mathf.Pow(rigidbody2D.velocity.magnitude, velocitySpinPower)) * Time.deltaTime);
        }
    }

    #endregion


    #region Collision

    /// <summary>
    /// 
    /// </summary>
    /// <param name="coll"></param>
    /// 

    void OnHitByBoomerang(Boomerang TheBoomShakingThisRoom)
    {
        if (TimeToDeath == -1)
        {
            gameObject.GetComponent<ParticleSystem>().Play(true);
            TimeToDeath = 3f;
            rigidbody2D.gravityScale = 1;
        }
    }
    #endregion

}